package com.example.flowabledemo;

import cn.hutool.core.collection.CollectionUtil;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class FlowableTestsForListener {

    @Autowired
    ProcessEngineConfiguration processEngineConfiguration;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Test
    void contextLoads() {

        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/flowable_taskListener02.bpmn20.xml")
                .name("任务监听器测试helloworld01")
                .deploy();
        System.out.println(deploy.getId());
    }

    /**
     * 启动流程 （创建流程实例...）
     * 已被挂起的流程 启动会报错
     */
    @Test
    void startProcess(){
        String processDefId = "eaf974e1-171a-11ef-a5db-a08069abebfe";
        //创建流程实例的时候 放置变量  这个变量为全局变量
        Map<String,Object> map =new HashMap<>();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefId);
        System.out.println(processInstance);
    }

    /**
     * 候选者找到任务  并拾取任务
     */
    @Test
    void howToFindTask(){
        String username ="zhangsan";
        //关羽登录之后  如何知道我现在有个候选者任务
        List<Task> taskList = taskService.createTaskQuery()
                .taskCandidateOrAssigned(username)
                .list();
        if (CollectionUtil.isEmpty(taskList)){
            System.out.println(username+"当前没有候选者任务");
            return;
        }

        for (Task task : taskList) {
            //候选者拾取任务
            taskService.claim(task.getId(),username);

        }
    }

    /**
     *
     */
    @Test
    void  unClaimTask(){
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("关羽")
                .list();
        for (Task task : taskList) {
            //取消拾取任务 并重新候选者拾取
            // taskService.unclaim(task.getId());
            //直接 指派一个人去交接。
            task.setAssignee("老六");
        }
    }




    /**
     *
     */
    @Test
    void findTask(){
        Task task = taskService.createTaskQuery()
                .taskCandidateUser("关羽")
                .singleResult();
        task.setAssignee("关羽");
    }

    @Test
    void waitTodoTask(){
        //查询张三的待办任务
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("zhangsan")
                .list();
        for (Task task : taskList) {
            System.out.println("任务id==>"+task.getId());
            taskService.setVariable(task.getId(),"on-running:taskVariable","taskValue" );
            taskService.setVariableLocal(task.getId(), "on-running:taskLocalVariable","taskLocalVariable");
            runtimeService.setVariable(task.getExecutionId(), "on-going:run:variable", "runValue");
            runtimeService.setVariableLocal(task.getExecutionId(), "on-going:run:localVariable", "runLocalValue");
        }
    }

    /**
     * 完成任务
     */
    @Test
    void finishTask(){
        Map<String,Object> map =new HashMap<>();

        taskService.complete("d7d10f42-171c-11ef-95c4-a08069abebfe");
    }

    /**
     * 流程定义的挂起 和流程实例的挂起
     */
    @Test
    void suspendAndActivity(){
        String processDefId = "firstFlow:2:3f1803b7-0e9c-11ef-9346-a08069abebfe";
        ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefId);
        boolean suspended = processDefinition.isSuspended();
        if (suspended){
            System.out.println("去激活");
            repositoryService.activateProcessDefinitionById(processDefId);
        }else{
            System.out.println("去挂起");
            repositoryService.suspendProcessDefinitionById(processDefId);
        }
    }

    @Test
    void suspendAndActivityInstance(){
        //挂起 激活xx
        runtimeService.activateProcessInstanceById("流程实例id");
        runtimeService.suspendProcessInstanceById("流程实例id");
    }

}
